import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { FileAppApi } from '#/api/infra/fileapp';
import type { FileCategoriesApi } from '#/api/infra/filecategories';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { handleTree } from '@vben/utils';

import { z } from '#/adapter/form';
import { getFileConfigSimpleList } from '#/api/infra/file-config';
import { getFileCategoriesList } from '#/api/infra/filecategories';
import { getOAuth2ClientList } from '#/api/system/oauth2/client';
import { DictBadge } from '#/components/dict-badge';
import { DictTag } from '#/components/dict-tag';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'parentId',
      label: '上级分类',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          const data = await getFileCategoriesList({});
          data.unshift({
            id: 0,
            name: '顶级文件分类',
          });
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择上级文件对象分类',
        treeDefaultExpandAll: true,
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'code',
      label: '分类编码',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入分类编码',
      },
    },
    {
      fieldName: 'name',
      label: '分类名称',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入分类名称',
      },
    },
    {
      component: 'ApiSelect',
      fieldName: 'configId',
      label: '环境配置',
      componentProps: {
        api: () => getFileConfigSimpleList({} as any),
        labelField: 'name',
        valueField: 'id',
        allowClear: true,
        placeholder: '请选择环境配置',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },

    {
      fieldName: 'encFlag',
      label: '是否加密',
      component: 'Switch',
      defaultValue: false,
      help: '请选择默认是否加密',
      componentProps: {
        class: '',
        checkedChildren: '是',
        unCheckedChildren: '否',
      },
    },
    {
      fieldName: 'securityLevel',
      label: '保密级别',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_SECURITY_LEVEL, 'number'),
        placeholder: '请选择保密级别',
      },
    },
    {
      fieldName: 'maxSize',
      label: '最大上传大小',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入最大上传文件大小：kb',
      },
      suffix: () => 'KB',
    },
    {
      fieldName: 'archiveDuration',
      label: '最大保存时间',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '最大保存天数，0表示永久保存',
      },
      suffix: () => '天',
    },
    {
      fieldName: 'sort',
      label: '显示顺序',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入显示顺序',
      },
    },
    {
      fieldName: 'basePath',
      label: '存放基础路径',
      component: 'Input',
      componentProps: {
        placeholder: '根据编码自动生成路径',
        disabled: true,
      },
    },
    {
      fieldName: 'fileExt',
      label: '可上传的文件类型',
      component: 'Select',
      componentProps: {
        mode: 'multiple',
        options: getDictOptions(DICT_TYPE.INFRA_FILE_EXT, 'string'),
        placeholder: '请选择可上传的文件后缀（类型）',
      },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入备注',
      },
    },
  ];
}
/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '分类编码',
      field: 'code',
    },
    {
      label: '分类名称',
      field: 'name',
    },
    {
      label: '上级分类',
      field: 'parentName',
    },
    {
      label: '配置名称',
      field: 'configName',
    },
    {
      label: '状态',
      field: 'status',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.COMMON_STATUS,
          value: val,
        });
      },
    },
    {
      label: '显示顺序',
      field: 'sort',
    },

    {
      label: '默认是否加密',
      field: 'encFlag',
      render: (val) =>
        h(DictBadge, {
          value: val,
        }),
    },
    {
      label: '保密级别',
      field: 'securityLevelName',
    },
    {
      label: '最大上传文件大小kb',
      field: 'maxSize',
    },
    {
      label: '最大存放天数',
      field: 'archiveDuration',
    },
    {
      label: '存放基础路径',
      field: 'basePath',
      span: 2,
    },
    {
      label: '可上传的文件类型',
      field: 'fileExt',
      span: 2,
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.INFRA_FILE_EXT,
          value: val,
        }),
    },
    {
      label: '备注',
      field: 'remark',
      span: 2,
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
  ];
}
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '分类编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入分类编码',
      },
    },
    {
      fieldName: 'name',
      label: '分类名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入分类名称',
      },
    },
    {
      fieldName: 'parentId',
      label: '上级分类',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          const data = await getFileCategoriesList({});
          data.unshift({
            id: 0,
            name: '顶级文件分类',
          });
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择上级文件对象分类',
        treeDefaultExpandAll: true,
      },
    },
    {
      component: 'ApiSelect',
      fieldName: 'configId',
      label: '环境配置',
      componentProps: {
        api: () => getFileConfigSimpleList({} as any),
        labelField: 'name',
        valueField: 'id',
        allowClear: true,
        placeholder: '请选择环境配置',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择状态（0正常 1停用）',
      },
    },
    {
      fieldName: 'securityLevel',
      label: '保密级别',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.INFRA_SECURITY_LEVEL, 'number'),
        placeholder: '请选择默认文件保密级别',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<FileCategoriesApi.FileCategories>['columns'] {
  return [
    {
      type: 'seq',
      title: '序号',
      minWidth: 60,
      fixed: 'left',
      align: 'left',
    },
    {
      field: 'id',
      title: '分类ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'name',
      title: '分类名称',
      minWidth: 120,
      fixed: 'left',
      align: 'left',
      treeNode: true,
    },
    {
      field: 'code',
      title: '分类编码',
      minWidth: 120,
    },
    {
      field: 'parentId',
      title: '上级分类',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'configName',
      title: '配置环境',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'isDefault',
      title: '默认分类',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_BOOLEAN_STRING },
      },
    },
    {
      field: 'sort',
      title: '显示顺序',
      minWidth: 120,
    },
    {
      field: 'basePath',
      title: '存放基础路径',
      minWidth: 120,
    },
    {
      field: 'encFlag',
      title: '默认是否加密',
      minWidth: 120,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'securityLevel',
      title: '默认文件安全级别',
      minWidth: 120,
    },
    {
      field: 'maxSize',
      title: '上传大小：kb',
      minWidth: 120,
    },
    {
      field: 'archiveDuration',
      title: '最大保存天数',
      minWidth: 120,
    },
    {
      field: 'fileExt',
      title: '文件后缀（类型）',
      minWidth: 120,
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
    },
    {
      field: 'actions',
      title: '操作',
      width: 280,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 新增/修改的表单 */
export function fileappUseFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'appId',
      label: '应用名称',
      component: 'ApiSelect',
      rules: 'required',
      componentProps: {
        api: async () => await getOAuth2ClientList(),
        labelField: 'name',
        valueField: 'id',
        allowClear: false,
        placeholder: '请选择应用',
      },
    },
    {
      fieldName: 'clientId',
      label: '客户端编号',
      rules: 'required',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'fileCategoriesId',
      label: '文件分类id',
      rules: 'required',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入文件分类id',
      },
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'readEnabled',
      label: '是否可读',
      component: 'Switch',
      defaultValue: false,
      help: '请选择默认是否可读',
      componentProps: {
        class: '',
        checkedChildren: '是',
        unCheckedChildren: '否',
      },
    },
    {
      fieldName: 'writeEnabled',
      label: '是否可写',
      component: 'Switch',
      defaultValue: false,
      help: '请选择默认是否可写',
      componentProps: {
        class: '',
        checkedChildren: '是',
        unCheckedChildren: '否',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
  ];
}

/** 列表的字段 */
export function fileappUseGridColumns(): VxeTableGridOptions<FileAppApi.FileApp>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'appName',
      title: '应用名称',
      minWidth: 120,
    },
    {
      field: 'clientId',
      title: '客户端编号',
      minWidth: 120,
    },
    {
      field: 'readEnabled',
      title: '是否可读',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_BOOLEAN_STRING },
      },
    },
    {
      field: 'writeEnabled',
      title: '是否可写',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_BOOLEAN_STRING },
      },
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
